package com.huige.wiki.job;

import com.huige.wiki.service.DocService;
import com.huige.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 文档定时任务
 */
@Component
public class DocJob {
    private static final Logger LOG = LoggerFactory.getLogger(DocJob.class);

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private DocService docService;

    /**
     * 每五分钟执行一次。
     * 每小时的第0秒，从第0分钟开始每隔5分钟执行一次任务
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void cron() {
        // 增加日志流水号码
        MDC.put("LOG_ID", String.valueOf(snowFlake.nextId()));

        LOG.info("开始更新电子书下的文档数据");
        long start = System.currentTimeMillis();
        docService.updateEbookInfo();
        LOG.info("文档数据更新结束, 耗时: {}毫秒",System.currentTimeMillis() - start);
    }

}
